# Table B.5: Marginal Effects of Ordered Net Benefits on Turnout


# 1. Load Packages ----

library(tidyverse)
library(stargazer)
library(margins)
library(miceadds)
library(xtable)

# 2. Read in Data ----

load(file = "df_voxit_individual_analysis.RData")

# 3. Conduct Regressions ----
l_p1_vcov <- glm.cluster(formula = turnout/100 ~ utility_prop_1st +
                           factor(year) + factor(canton_id)+
                           male + married + age + uni + knowledge + leftright +
                           initiative + initiative_counter + referendum_fak + 
                           referendum_comp ,
                         cluster = as.numeric(voxit_individual[voxit_individual$prop_voxit==1,]$datum_merge) + 
                           voxit_individual[voxit_individual$prop_voxit==1,]$canton_id,
                         family = binomial(link = "logit"), 
                         data = voxit_individual[voxit_individual$prop_voxit==1,])
l_p1 <- glm(formula = turnout/100 ~ utility_prop_1st +
              factor(year) + factor(canton_id)+ 
              male + married + age + uni + knowledge + leftright +
              initiative + initiative_counter + referendum_fak + 
              referendum_comp ,
            family = binomial(link = "logit"), 
            data = voxit_individual[voxit_individual$prop_voxit==1,])
marg_prop1 <- summary(margins(model = l_p1, vcov = l_p1_vcov$vcov, 
                              variables = c("utility_prop_1st")))

l_p2_vcov <- glm.cluster(formula = turnout/100 ~ utility_prop_1st + utility_prop_2nd +
                           factor(year) + factor(canton_id)+ 
                           male + married + age + uni + knowledge + leftright +
                           initiative + initiative_counter + referendum_fak + 
                           referendum_comp ,
                         cluster = as.numeric(voxit_individual[voxit_individual$prop_voxit==2,]$datum_merge) + 
                           voxit_individual[voxit_individual$prop_voxit==2,]$canton_id,
                         family = binomial(link = "logit"), 
                         data = voxit_individual[voxit_individual$prop_voxit==2,])
l_p2 <- glm(formula = turnout/100 ~ utility_prop_1st + utility_prop_2nd +
              factor(year) + factor(canton_id)+ 
              male + married + age + uni + knowledge + leftright +
              initiative + initiative_counter + referendum_fak + 
              referendum_comp ,
            family = binomial(link = "logit"), 
            data = voxit_individual[voxit_individual$prop_voxit==2,])
marg_prop2 <- summary(margins(model = l_p2, vcov = l_p2_vcov$vcov, 
                              variables = c("utility_prop_1st","utility_prop_2nd")))

l_p3_vcov <- glm.cluster(formula = turnout/100 ~ utility_prop_1st + utility_prop_2nd +
                           utility_prop_3rd +
                           factor(year) + factor(canton_id)+ 
                           male + married + age + uni + knowledge + leftright +
                           initiative + initiative_counter + referendum_fak + 
                           referendum_comp ,
                         cluster = as.numeric(voxit_individual[voxit_individual$prop_voxit==3,]$datum_merge) + 
                           voxit_individual[voxit_individual$prop_voxit==3,]$canton_id,
                         family = binomial(link = "logit"), 
                         data = voxit_individual[voxit_individual$prop_voxit==3,])
l_p3 <- glm(formula = turnout/100 ~ utility_prop_1st + utility_prop_2nd + utility_prop_3rd +
              factor(year) + factor(canton_id)+ 
              male + married + age + uni + knowledge + leftright +
              initiative + initiative_counter + referendum_fak + 
              referendum_comp ,
            family = binomial(link = "logit"), 
            data = voxit_individual[voxit_individual$prop_voxit==3,])
marg_prop3 <- summary(margins(model = l_p3, vcov = l_p3_vcov$vcov, 
                              variables = c("utility_prop_1st","utility_prop_2nd", 
                                            "utility_prop_3rd")))

l_p4_vcov <- glm.cluster(formula = turnout/100 ~ utility_prop_1st + utility_prop_2nd +
                           utility_prop_3rd + utility_prop_4th +
                           factor(year) + factor(canton_id)+ 
                           male + married + age + uni + knowledge + leftright +
                           initiative + initiative_counter + referendum_fak + 
                           referendum_comp ,
                         cluster = as.numeric(voxit_individual[voxit_individual$prop_voxit==4,]$datum_merge) + 
                           voxit_individual[voxit_individual$prop_voxit==4,]$canton_id,
                         family = binomial(link = "logit"), 
                         data = voxit_individual[voxit_individual$prop_voxit==4,])
l_p4 <- glm(formula = turnout/100 ~ utility_prop_1st + utility_prop_2nd +
              utility_prop_3rd + utility_prop_4th +
              factor(year) + factor(canton_id)+ 
              male + married + age + uni + knowledge + leftright +
              initiative + initiative_counter + referendum_fak + 
              referendum_comp ,
            family = binomial(link = "logit"), 
            data = voxit_individual[voxit_individual$prop_voxit==4,])
marg_prop4 <- summary(margins(model = l_p4, vcov = l_p4_vcov$vcov, 
                              variables = c("utility_prop_1st","utility_prop_2nd", 
                                            "utility_prop_3rd", "utility_prop_4th")))

l_p5_vcov <- glm.cluster(formula = turnout/100 ~ utility_prop_1st + utility_prop_2nd +
                           utility_prop_3rd + utility_prop_4th + utility_prop_5th +
                           factor(year) + factor(canton_id)+ 
                           male + married + age + uni + knowledge + leftright +
                           initiative + initiative_counter + referendum_fak + 
                           referendum_comp ,
                         cluster = as.numeric(voxit_individual[voxit_individual$prop_voxit==5,]$datum_merge) + 
                           voxit_individual[voxit_individual$prop_voxit==5,]$canton_id,
                         family = binomial(link = "logit"), 
                         data = voxit_individual[voxit_individual$prop_voxit==5,])
l_p5 <- glm(formula = turnout/100 ~ utility_prop_1st + utility_prop_2nd +
              utility_prop_3rd + utility_prop_4th + utility_prop_5th +
              factor(year) + factor(canton_id)+ 
              male + married + age + uni + knowledge + leftright +
              initiative + initiative_counter + referendum_fak + 
              referendum_comp ,
            family = binomial(link = "logit"), 
            data = voxit_individual[voxit_individual$prop_voxit==5,])
marg_prop5 <- summary(margins(model = l_p5, vcov = l_p5_vcov$vcov, 
                              variables = c("utility_prop_1st","utility_prop_2nd", 
                                            "utility_prop_3rd", "utility_prop_4th",
                                            "utility_prop_5th")))

l_p6_vcov <- glm.cluster(formula = turnout/100 ~ utility_prop_1st + utility_prop_2nd +
                           utility_prop_3rd + utility_prop_4th + utility_prop_5th +
                           utility_prop_6th +
                           factor(year) + factor(canton_id)+ 
                           male + married + age + uni + knowledge + leftright +
                           initiative + initiative_counter + referendum_fak + 
                           referendum_comp ,
                         cluster = as.numeric(voxit_individual[voxit_individual$prop_voxit==6,]$datum_merge),
                         family = binomial(link = "logit"), 
                         data = voxit_individual[voxit_individual$prop_voxit==6,])
l_p6 <- glm(formula = turnout/100 ~ utility_prop_1st + utility_prop_2nd +
              utility_prop_3rd + utility_prop_4th + utility_prop_5th +
              utility_prop_6th +
              factor(year) + factor(canton_id)+ 
              male + married + age + uni + knowledge + leftright +
              initiative + initiative_counter + referendum_fak + 
              referendum_comp ,
            family = binomial(link = "logit"), 
            data = voxit_individual[voxit_individual$prop_voxit==6,])
marg_prop6 <- summary(margins(model = l_p6, vcov = l_p6_vcov$vcov, 
                              variables = c("utility_prop_1st","utility_prop_2nd", 
                                            "utility_prop_3rd", "utility_prop_4th",
                                            "utility_prop_5th", "utility_prop_6th")))

margx_empty[[2,5]] #***
Net_benefit <- c(paste0(round(margx_empty[[2,2]]*100, 3),"***"))
Net_benefit_se <- c(round(margx_empty[[2,3]]*100,3))
# 4. Regression Table ----
## 4.1 U_1st ----
U_1 <- c(marg_prop1[[2]][[1]], marg_prop2[[2]][[1]], marg_prop3[[2]][[1]], 
         marg_prop4[[2]][[1]], marg_prop5[[2]][[1]], marg_prop6[[2]][[1]])
U_1_se <- c(marg_prop1[[3]][[1]], marg_prop2[[3]][[1]], marg_prop3[[3]][[1]], 
            marg_prop4[[3]][[1]], marg_prop5[[3]][[1]], marg_prop6[[3]][[1]])
# p-values
t1 <- U_1/U_1_se
for (i in 1:6) {
  print(2*pt(-abs(t1[i]), length(unique(voxit_individual[voxit_individual$prop_voxit==i,]$datum_merge))-1))
}

# Coefficients
U_1st <- c(paste0(round(marg_prop1[[2]][[1]]*100, 3),"***"),
           paste0(round(marg_prop2[[2]][[1]]*100, 3),"***"),
           paste0(round(marg_prop3[[2]][[1]]*100, 3),"***"),
           paste0(round(marg_prop4[[2]][[1]]*100, 3),"***"),
           paste0(round(marg_prop5[[2]][[1]]*100, 3),"***"),
           paste0(round(marg_prop6[[2]][[1]]*100, 3),"**"))
U_1st_se <- c(round(marg_prop1[[3]][[1]]*100, 3),
              round(marg_prop2[[3]][[1]]*100, 3),
              round(marg_prop3[[3]][[1]]*100, 3),
              round(marg_prop4[[3]][[1]]*100, 3),
              round(marg_prop5[[3]][[1]]*100, 3),
              round(marg_prop6[[3]][[1]]*100, 3))

## 4.2 U_2nd ----
U_2 <- c(NA, marg_prop2[[2]][[2]], marg_prop3[[2]][[2]], 
         marg_prop4[[2]][[2]], marg_prop5[[2]][[2]], marg_prop6[[2]][[2]])
U_2_se <- c(NA, marg_prop2[[3]][[2]], marg_prop3[[3]][[2]], 
            marg_prop4[[3]][[2]], marg_prop5[[3]][[2]], marg_prop6[[3]][[2]])
#p-values
t2 <- U_2/U_2_se
for (i in 1:6) {
  print(2*pt(-abs(t2[i]), length(unique(voxit_individual[voxit_individual$prop_voxit==i,]$datum_merge))-1))
}

# Coefficients
U_2nd <- c(NA,
           paste0(round(marg_prop2[[2]][[2]]*100, 3),"***"),
           paste0(round(marg_prop3[[2]][[2]]*100, 3),"***"),
           paste0(round(marg_prop4[[2]][[2]]*100, 3),"*"),
           paste0(round(marg_prop5[[2]][[2]]*100, 3),"**"),
           paste0(round(marg_prop6[[2]][[2]]*100, 3),""))
U_2nd_se <- c(NA,
              round(marg_prop2[[3]][[2]]*100, 3),
              round(marg_prop3[[3]][[2]]*100, 3),
              round(marg_prop4[[3]][[2]]*100, 3),
              round(marg_prop5[[3]][[2]]*100, 3),
              round(marg_prop6[[3]][[2]]*100, 3))

## 4.3 U_3rd ----
U_3 <- c(NA, NA, marg_prop3[[2]][[3]], 
         marg_prop4[[2]][[3]], marg_prop5[[2]][[3]], marg_prop6[[2]][[3]])
U_3_se <- c(NA, NA, marg_prop3[[3]][[3]], 
            marg_prop4[[3]][[3]], marg_prop5[[3]][[3]], marg_prop6[[3]][[3]])
#p-values
t3 <- U_3/U_3_se
for (i in 1:6) {
  print(2*pt(-abs(t3[i]), length(unique(voxit_individual[voxit_individual$prop_voxit==i,]$datum_merge))-1))
}

# Coefficients
U_3rd <- c(NA,
           NA,
           paste0(round(marg_prop3[[2]][[3]]*100, 3),"***"),
           paste0(round(marg_prop4[[2]][[3]]*100, 3),""),
           paste0(round(marg_prop5[[2]][[3]]*100, 3),""),
           paste0(round(marg_prop6[[2]][[3]]*100, 3),""))
U_3rd_se <- c(NA,
              NA,
              round(marg_prop3[[3]][[3]]*100, 3),
              round(marg_prop4[[3]][[3]]*100, 3),
              round(marg_prop5[[3]][[3]]*100, 3),
              round(marg_prop6[[3]][[3]]*100, 3))

## 4.4 U_4th ----
U_4 <- c(NA, NA, NA, 
         marg_prop4[[2]][[4]], marg_prop5[[2]][[4]], marg_prop6[[2]][[4]])
U_4_se <- c(NA, NA, NA, 
            marg_prop4[[3]][[4]], marg_prop5[[3]][[4]], marg_prop6[[3]][[4]])
#p-values
t4 <- U_4/U_4_se
for (i in 1:6) {
  print(2*pt(-abs(t4[i]), length(unique(voxit_individual[voxit_individual$prop_voxit==i,]$datum_merge))-1))
}

# Coefficients
U_4th <- c(NA,
           NA,
           NA,
           paste0(round(marg_prop4[[2]][[4]]*100, 3),"**"),
           paste0(round(marg_prop5[[2]][[4]]*100, 3),"**"),
           paste0(round(marg_prop6[[2]][[4]]*100, 3),""))
U_4th_se <- c(NA,
              NA,
              NA,
              round(marg_prop4[[3]][[4]]*100, 3),
              round(marg_prop5[[3]][[4]]*100, 3),
              round(marg_prop6[[3]][[4]]*100, 3))

## 4.5 U_5th ----
U_5 <- c(NA, NA, NA, 
         NA, marg_prop5[[2]][[5]], marg_prop6[[2]][[5]])
U_5_se <- c(NA, NA, NA,  
            NA, marg_prop5[[3]][[5]], marg_prop6[[3]][[5]])
#p-values
t5 <- U_5/U_5_se
for (i in 1:6) {
  print(2*pt(-abs(t5[i]), length(unique(voxit_individual[voxit_individual$prop_voxit==i,]$datum_merge))-1))
}

# Coefficients
U_5th <- c(NA,
           NA,
           NA,
           NA,
           paste0(round(marg_prop5[[2]][[5]]*100, 3),"**"),
           paste0(round(marg_prop6[[2]][[5]]*100, 3),""))
U_5th_se <- c(NA,
              NA,
              NA,
              NA,
              round(marg_prop5[[3]][[5]]*100, 3),
              round(marg_prop6[[3]][[5]]*100, 3))

## 4.6 U_6th ----
U_6 <- c(NA, NA, NA,
         NA, NA, marg_prop6[[2]][[6]])
U_6_se <- c(NA, NA, NA,
            NA, NA, marg_prop6[[3]][[6]])
#p-value
t6 <- U_6/U_6_se
for (i in 1:6) {
  print(2*pt(-abs(t6[i]), length(unique(voxit_individual[voxit_individual$prop_voxit==i,]$datum_merge))-1))
}

# Coefficients
U_6th <- c(NA,
           NA,
           NA,
           NA,
           NA,
           paste0(round(marg_prop6[[2]][[6]]*100, 3),""))
U_6th_se <- c(NA,
              NA,
              NA,
              NA,
              NA,
              round(marg_prop6[[3]][[6]]*100, 3))

## 4.7 Create and Save Table ----
# Number of propositions
Number_of_propositions <- c(1,2,3,4,5,6)

# Number of voting days
Number_of_voting_days <- c(length(unique(voxit_individual[voxit_individual$prop_voxit==1,]$datum_merge)),
                           length(unique(voxit_individual[voxit_individual$prop_voxit==2,]$datum_merge)),
                           length(unique(voxit_individual[voxit_individual$prop_voxit==3,]$datum_merge)),
                           length(unique(voxit_individual[voxit_individual$prop_voxit==4,]$datum_merge)),
                           length(unique(voxit_individual[voxit_individual$prop_voxit==5,]$datum_merge)),
                           length(unique(voxit_individual[voxit_individual$prop_voxit==6,]$datum_merge)))

# Number of observations
Observations <- c(length(voxit_individual[voxit_individual$prop_voxit==1,]$datum_merge),
                  length(voxit_individual[voxit_individual$prop_voxit==2,]$datum_merge),
                  length(voxit_individual[voxit_individual$prop_voxit==3,]$datum_merge),
                  length(voxit_individual[voxit_individual$prop_voxit==4,]$datum_merge),
                  length(voxit_individual[voxit_individual$prop_voxit==5,]$datum_merge),
                  length(voxit_individual[voxit_individual$prop_voxit==6,]$datum_merge))

# Put everything into one table
coeffs <- rbind(U_1st, U_1st_se, U_2nd, U_2nd_se, U_3rd, U_3rd_se, U_4th, U_4th_se, 
                U_5th, U_5th_se, U_6th, U_6th_se, Number_of_propositions,
                Number_of_voting_days, Observations)

# Save it
print(xtable(coeffs), file = "TableB5.tex")













